package com.ustb.hospital.servlet;

import com.ustb.hospital.entity.admins;
import com.ustb.hospital.service.AdminsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class AdminsLoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminsServiceImpl adminsService = new AdminsServiceImpl();
        admins admins = adminsService.queryByUsername(username);
        if(admins == null) {
            System.out.println("notfound");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }else{
            if(!admins.getPassword().equals(password))
            {
                System.out.println("Password error");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
            else{
                System.out.println("Success!");
                resp.sendRedirect("/list");
            }
        }
    }
}
